/*
"Replication Data for: The real-time impact of the war on Russian imports: 
a synthetic control method approach" by Mancini, Conteduca, Borin 

Fig1-2-3.do: The files print Figures 1 and 2 of the paper and export 
the underlying data of figures 1, 2, and 3 to dta files

*/

clear  



 
use "${main_loc}/data_baseline_WTR",replace

************************************************************************************************************************
**# LOOP
************************************************************************************************************************
 

drop if partner=="Ukraine"	
drop if partner=="Turkey"	
drop if partner=="Kazakhstan"	
drop if partner=="Belarus"	
drop if partner=="Serbia"	
drop if partner=="Armenia"
drop if partner=="Uzbekistan"
drop if partner=="Kyrgyzstan"
drop if partner=="Georgia"
drop if partner=="Azerbaijan"


levelsof iso2_o, local(levels) 

foreach c of local levels{
preserve

keep if iso2_o=="`c'"
 

	di "`c'"
 merge n:1 reporter partner using "${main_loc}/export_similarities_Russia_WTR.dta"
capture drop s1_* s2_* s3_* s4_* s5_*  
drop if _m!=3
replace s1=0 if s1==.
replace s2=0 if s2==.
replace s3=0 if s3==.
replace s4=0 if s4==.
replace s5=0 if s5==.

egen id=group(iso2_d),label
egen t=group(date)
tsset id t

drop if BVlndist==.
drop if BVcontiguity==.
drop if BVcommon_language==.
drop if lnagdp_wdi_const==.
 
tsfill,full
replace value=0 if value==.

sort id t
local varl  BVlndist BVcontiguity BVcommon_language lnagdp_wdi_const  max_agree_pta_goods max_agree_cu 
foreach x of local varl {  
sort id `x' 
by id: carryforward `x' ,replace
}

 
sort id t
gen date_new=date  
order id date date_new 

bys id: replace date_new=l.date_new+1 if date_new==.
bys id: replace date_new=l2.date_new+2 if date_new==.
bys id: replace date_new=l3.date_new+3 if date_new==.
bys id: replace date_new=l4.date_new+4 if date_new==.
bys id: replace date_new=l5.date_new+5 if date_new==.
bys id: replace date_new=f.date_new-1 if date_new==.
bys id: replace date_new=f2.date_new-2 if date_new==.
bys id: replace date_new=f3.date_new-3 if date_new==.
bys id: replace date_new=f4.date_new-4 if date_new==.
bys id: replace date_new=f5.date_new-5 if date_new==.
format date_new %tm
drop date fdate year  month
rename date_new date

gen fdate = dofm(date)
gen year=year(fdate)
gen month=month(fdate)
gen dm=month



 
 keep if t>=13

sort id t

summ id if partner=="Russia"
   synth   value   value BVlndist BVcontiguity BVcommon_language  lnagdp_wdi_const  s1 s2  s3  s4  max_agree_pta_goods ,   trunit("`r(mean)'") trperiod(38)    keep("`c'_Russia",replace)  
 summ id if partner=="Russia"
  synth_asmd, trunit("`r(mean)'") trperiod(38) depvar(value) data("`c'_Russia.dta")
sca `c'_Russia_mean=r(mean)
sca test=r(mean)
gen soglia=0.4
sca `c'_Russia_max=r(max)
sum soglia
if test>r(mean) {
capture erase "`c'_Russia.dta"  
 }
 

 restore
}

 

 foreach c of local levels {   
    clear 
capture use "${main_loc}/`c'_Russia.dta"
capture keep _Y_treated _Y_synthetic _time
capture gen iso2_o="`c'"
capture drop if iso2_o==""
capture drop if _time==.

capture save  "${main_loc}/`c'_pre_append_Russia.dta",replace
}
  
 
  

 
  
clear
cd "${main_loc}"
local theFiles: dir . files "*_pre_append_Russia.dta" 

append using `theFiles' 
 
**********
gen keep=1 if _time==48
bys iso2_o: egen insample=max(keep)
drop if insample==.
drop if _time>50
egen id=group(iso2_o)
tsset id _time
 
drop if iso2_o=="UA"
 
***FIGURE 1 and 2
preserve
rename iso2_o iso2
merge n:1 iso2 using "${main_loc}/sanctioning_list_WTR.dta" 
drop if _m == 2
drop _m
replace sanctioning=0 if sanctioning==.
collapse (sum) _Y_treated _Y_synthetic,by(_time sanctioning)
reshape wide _Y_treated _Y_synthetic, i(_time) j(sanctioning)
gen diffp0=((_Y_treated0-_Y_synthetic0)/_Y_synthetic0)*100
gen diffp1=((_Y_treated1-_Y_synthetic1)/_Y_synthetic1)*100
*Figure 2
tsline diffp0 diffp1
graph export "${main_loc}/Figure_2.png",replace
*Figure 1
gen _Y_treated_tot=_Y_treated0+_Y_treated1
gen _Y_synthetic_tot=_Y_synthetic0+_Y_synthetic1
tsline _Y_treated_tot _Y_synthetic_tot
graph export "${main_loc}/Figure_1.png",replace
save "${main_loc}/Figure_1_2.dta",replace
restore

***FIGURE 3
preserve
gen period=0 if _time>=39 & _time<=40
replace period=1 if _time>=46 & _time<=48
collapse (sum) _Y_treated _Y_synthetic,by(id iso2_o period)
drop if period==.
reshape wide _Y_treated _Y_synthetic, i(iso2_o) j(period)
gen impact_perio0=_Y_treated0/_Y_synthetic0*100-100
gen impact_perio1=_Y_treated1/_Y_synthetic1*100-100
keep iso2_o impact_perio0 impact_perio1
save "${main_loc}/Figure_3.dta",replace
restore
 
 
